package com.cf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cf.domain.ResponseResult;
import com.cf.domain.pojo.Judges;
import com.cf.domain.pojo.PageQuery;
import com.cf.domain.pojo.ScoreOptions;
import com.cf.service.IJudgesService;
import com.cf.service.IScoreOptionsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author lzqinga@126.com
 * @Date 2024/11/9  10:06
 */
@Controller
@RequiredArgsConstructor
@RequestMapping("/option")
public class ScoreOptionsController {
    private final IScoreOptionsService scoreOptionsService;
    /**
     * 添加
     */
    @PostMapping
    @ResponseBody
    public ResponseResult save(@RequestBody ScoreOptions scoreOptions){
        //如果编号是2位则level为1；如果编号为4为则level为2
        if(scoreOptions.getCode().length() == 2){
            scoreOptions.setLevel(1);
        }else if(scoreOptions.getCode().length() == 4){
            scoreOptions.setLevel(2);
        }
        //根据编号判断级别
        scoreOptionsService.save(scoreOptions);
        return ResponseResult.sucess();
    }

    @PutMapping
    @ResponseBody
    public ResponseResult update(@RequestBody ScoreOptions scoreOptions){
        scoreOptionsService.updateById(scoreOptions);
        return ResponseResult.sucess();
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseResult getById(@PathVariable Long id){
        ScoreOptions scoreOptions = scoreOptionsService.getById(id);
        return ResponseResult.sucess(scoreOptions);
    }

    @GetMapping("/pagination")
    @ResponseBody
    public ResponseResult getAll(@RequestParam(defaultValue = "0") Integer pageNo,
                                 @RequestParam(defaultValue = "10") Integer pageSize){
        PageQuery pageQuery = new PageQuery(Integer.valueOf(pageNo), pageSize);
        Page<ScoreOptions> data = scoreOptionsService.selectAllScoreOptions(pageQuery);
        return ResponseResult.sucess(data);
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseResult deleteById(@PathVariable Long id){
        ScoreOptions scoreOptions = new ScoreOptions();
        scoreOptions.setId(id);
        scoreOptions.setEnable(1);
        scoreOptionsService.updateById(scoreOptions);
        return ResponseResult.sucess();
    }

    @GetMapping("/detail/{type}")
    @ResponseBody
    public ResponseResult getByType(@PathVariable Integer type){
        List<ScoreOptions> data = scoreOptionsService.listByType(type);
        return ResponseResult.sucess(data);
    }

}
